//
//  SystemDatabase.h
//  Phidgets
//
//  Created by Daniel Hazelbaker on 11/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#ifndef __SYSTEMDATABASE_H__
#define __SYSTEMDATABASE_H__

#include <sqlite3.h>
#include <pthread.h>
#include <map>
#include <string>
#include "SqlValue.h"
#include "SqlStatement.h"


class SystemDatabase
{
private:
    sqlite3 *_database;
	pthread_mutex_t _lockMutex;
    pthread_mutexattr_t _lockMutexAttr;
    
    void PrepareDatabase();
    
public:
    SystemDatabase(std::string dbFile);
    ~SystemDatabase();
    
    bool ExecuteSimpleNonQuery(std::string sql);
    SqlStatement *prepareStatement(std::string sql, SqlParameters params);
    
    /* Prevent copying. */
    SystemDatabase(const SystemDatabase&);
    SystemDatabase& operator=(const SystemDatabase&);
    
    void Lock() { pthread_mutex_lock(&_lockMutex); }
    void Unlock() { pthread_mutex_unlock(&_lockMutex); }
    
    bool TableExists(std::string tableName);
    unsigned long LastInsertRowID() const;
};


extern SystemDatabase *gSharedDatabase;

#endif /* __SYSTEMDATABASE_H__ */
